package alg20241219;

import java.util.Random;

public class Text2 {
    public int[] sortArray(int[] nums) {

        int n = nums.length;
        quick(nums, 0, n - 1);
        return nums;
    }
    public void quick(int[] nums, int l, int r){
        if(l >= r) return;
        int left = l - 1, right = r + 1, i = l;
        int key = nums[new Random().nextInt(r - l + 1) + l];
        while(i < right){
            if(nums[i] < key) swap(nums, ++left, i++);
            else if(nums[i] == key) i++;
            else swap(nums, --right, i);
        }
        quick(nums, l, left);
        quick(nums, right, r);
    }

    public void swap(int[] nums, int i, int j){
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}
